package com.falling.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.falling.reggie.common.R;
import com.falling.reggie.dto.DishDto;
import com.falling.reggie.entity.Category;
import com.falling.reggie.entity.Dish;
import com.falling.reggie.entity.DishFlavor;
import com.falling.reggie.service.CategoryService;
import com.falling.reggie.service.DishFlavorService;
import com.falling.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Resource
    private DishService dishService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private DishFlavorService dishFlavorService;


    @GetMapping("/{id}")
    public R<DishDto> revealInfo(@PathVariable Long id){

        DishDto dishDto = dishService.getByIdWithFlavor(id);
        if (dishDto != null) return R.success(dishDto);
        else return R.error("reveal info fail...");

    }

    @GetMapping("/page")
    public R<Page<DishDto>> page(int page, int pageSize, String name){
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        Page<DishDto> dishDtoPage = new Page<>();

        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Dish::getName, name);
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        dishService.page(pageInfo, queryWrapper);

        // 对象拷贝，忽略records，因为之后要对其进行处理
        BeanUtils.copyProperties(pageInfo, dishDtoPage, "records");

        List<Dish> records = pageInfo.getRecords();
        List<DishDto> dishDtoList = records.stream().map((item) -> {
            DishDto dishDto = new DishDto();

            // 将records里的元素拷贝到临时对象dishDto中
            BeanUtils.copyProperties(item, dishDto);

            // 通过categoryId查category表，获取name，存入dishDto
            Category category = categoryService.getById(item.getCategoryId());
            String categoryName = category.getName();
            dishDto.setCategoryName(categoryName);

            return dishDto;
        }).collect(Collectors.toList());

        // 将处理好的records重新存入
        dishDtoPage.setRecords(dishDtoList);

        return R.success(dishDtoPage);

    }

    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        queryWrapper.eq(Dish::getStatus, 1);

        List<Dish> list = dishService.list(queryWrapper);

        List<DishDto> dishDtoList = list.stream().map((item) -> {
            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(item, dishDto);

            LambdaQueryWrapper<DishFlavor> flavorQueryWrapper = new LambdaQueryWrapper<>();
            flavorQueryWrapper.eq(DishFlavor::getDishId, item.getId());

            List<DishFlavor> dishFlavors = dishFlavorService.list(flavorQueryWrapper);
            dishDto.setFlavors(dishFlavors);

            return dishDto;
        }).collect(Collectors.toList());


        return R.success(dishDtoList);
    }


    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){

        if (dishService.updateWithFlavor(dishDto)) return R.success("update success...");
        else return R.error("update fail...");
    }

    @PostMapping
    public R<String> save(@RequestBody DishDto dto){
        if (dishService.saveWithFlavor(dto)) return R.success("save success...");
        else return R.error("save fail...");
    }

    @PostMapping("/status/{status}")
    public R<String> updateStatus(@RequestParam List<Long> ids, @PathVariable String status){
        UpdateWrapper<Dish> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", status).in("id", ids);
        if (dishService.update(updateWrapper)) return R.success("update status success...");
        else return R.error("update status fail...");
    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        if(dishService.removeByIds(ids)) return R.success("delete success...");
        else return R.error("delete fail...");
    }


}
